<?php

	/**
	 * Contains object validation functionality
	 * 
	 * @version 3.0
	 * @package Core
	 * @author Rostislav Rotaru
	 * 
	 */
	class Check
	{
		
		/* METHODS */
		
		/**
		 * Check if object is not null
		 * 
		 * @param mixed $object. Object to check.
		 */
		public static function IsNotNull($object)
		{
			if ($object == null)
			{
				throw new SpherusException(SpherusException::NULL_OBJECT);
			}
		}
		
		/**
		 * Check if object is not null or empty
		 * 
		 * @param mixed $object. Object to check.
		 */
		public static function IsNotNullOrEmpty($object)
		{
			if (!isset($object))
			{
				throw new SpherusException(SpherusException::NULL_OR_EMPTY_OBJECT);
			}
		}
		
		/**
		 * Check if given object is class type
		 * 
		 * @param mixed $object. Object to check.
		 * @param string $className. The name of class.
		 */
		public static function IsClassType($object, $className)
		{
			if ($object instanceof $className)
			{
				throw new SpherusException(sprintf(SpherusException::INVALID_CLASS_TYPE, $className));
			}
		}

		/**
		 * Check if given data is empty or null
		 *
		 * @param $value validation value to parse
		 *
		 * @return boolean true if empty or null, otherwise false
		 */
		public static function IsNonEmpty($value)
		{
			return !(($value == null) || ($value === ""));
		}

		/**
		 * Check if given data is valid email
		 *
		 * @param $value validation value to parse
		 *
		 * @return boolean true if valid email, otherwise false
		 */
		public static function IsEmail($value)
		{
			return filter_var($value, FILTER_VALIDATE_EMAIL);
		}

		/**
		 * Check if given data is integer
		 *
		 * @param $value validation value to parse
		 *
		 * @return boolean true if valid integer, otherwise false
		 */
		public static function IsInteger($value)
		{
			return filter_var($value, FILTER_VALIDATE_INT);
		}

		/**
		 * Check if given data is float
		 *
		 * @param $value validation value to parse
		 *
		 * @return boolean true if valid float, otherwise false
		 */
		public static function IsFloat($value)
		{
			return filter_var($value, FILTER_VALIDATE_FLOAT)||(empty($value) || (str_replace('.', '', str_replace('0', '', $value)) === ''));
		}

		/**
		 * Check if given data is URL
		 *
		 * @param $value validation value to parse
		 *
		 * @return boolean true if valid URL, otherwise false
		 */
		public static function IsUrl($value)
		{
			return filter_var($value, FILTER_VALIDATE_URL);
		}

		/**
		 * Check if given data is IP address
		 *
		 * @param $value validation value to parse
		 *
		 * @return boolean true if valid IP address (v4 and v6 are allowed), otherwise false
		 */
		public static function IsIp($value)
		{
			return filter_var($value, FILTER_VALIDATE_IP);
		}

		/**
		 * Check if given data is valid date
		 *
		 * @param $value validation value to parse
		 *
		 * @return boolean true if valid date, otherwise false
		 */
		public static function IsDate($value)
		{
			$parsedDate = date_parse($value);
			return checkdate($parsedDate['month'], $parsedDate['day'], $parsedDate['year']);
		}
		
		/**
		 * Checks whether the value is greater then rule
		 *
		 * @param $value value to be checked
		 * @param $rule validation rule value
		 * @return boolean
		 */
		public static function CheckMinValue($value, $rule)
		{
			return ($value >= $rule);
		}

		/**
		 * Checks whether the value is less then rule
		 *
		 * @param $value value to be checked
		 * @param $rule validation rule value
		 * @return boolean
		 */
		public static function CheckMaxValue($value, $rule)
		{
			return ($value <= $rule);
		}

		/**
		 * Checks whether the value length is greater then rule
		 *
		 * @param $value value to be checked
		 * @param $rule validation rule value
		 * @return boolean
		 */
		public static function CheckMinLength($value, $rule)
		{
			return (strlen($value) >= $rule);
		}

		/**
		 * Checks whether the value length is less then rule
		 *
		 * @param $value value to be checked
		 * @param $rule validation rule value
		 * @return boolean
		 */
		public static function CheckMaxLength($value, $rule)
		{
			return (strlen($value) <= $rule);
		}

		/**
		 * Checks whether the value corresponds the Perl-compatible regular expression rule
		 *
		 * @param $value value to be checked
		 * @param $rule validation rule Perl-compatible regular expression
		 * @return boolean
		 */
		public static function CheckRegExp($value, $rule)
		{
			return preg_match($rule, $value);
		}

	}

?>